<?php

namespace Proyecto\MainBundle\Services;

use Symfony\Component\Yaml\Parser;

class Configurador {
    
    private $configurations;
    private $titulo_default = 'Proyecto 2012';
    private $header_default = 'Administración de curriculum vitae';
    private $elementos_default = 4;
    private $footer_default = 'Yañez Ignacio, Giussi Gabriel';
    
    public function __construct(){
        try {
            $yaml = new Parser();
            $this->setConfigurations($yaml->parse(file_get_contents(__DIR__.'/configurations.yml')));
        } catch (ParseException $e) {
          printf("Unable to parse the YAML string: %s", $e->getMessage());
        }
    }
    
    private function getConfigurations(){
        return $this->configurations;
    }
    
    private function setConfigurations($configurations){
        $this->configurations=$configurations;
    }
    
    private function getTituloDefecto(){
        return $this->titulo_default;
    }
    
    private function getHeaderDefecto(){
        return $this->header_default;
    }
    
    private function getFooterDefecto(){
        return $this->footer_default;
    }
    
    private function getElementosDefault(){
        return $this->elementos_default;
    }
    
    
    public function getElementosPorPagina($tipo){
        if ($tipo){
            $indices = array('por_pagina',$tipo);
        }
        else{
            $indices = array('elementos_por_pagina');
        }
        $elementos_por_pagina = $this->getElemento($indices);
        if ($elementos_por_pagina){
            return $elementos_por_pagina;
        }
        else {
            return $this->getElementosDefault();
        }
    }
    
    public function getHeader($path = null,$user = null){
        if ($path){
            $indices = array('headers',$path);
        }
        else {
            $indices = array('header');
        }
        $header = $this->getElemento($indices);
        if ($header){
            return str_replace('%user%',$user,$header);
        }
        else {
            return $this->getHeaderDefecto();
        }
    }
    
    private function getElemento($indices){
        $found=false;
        $elementos = $this->getConfigurations(); 
        foreach ($indices as $indice){
            if (array_key_exists($indice,$elementos)){
                $elementos = $elementos[$indice];
                $found = true;
            }
            else{
                $found=false;
                break;
            }
        }
        if ($found){
            return $elementos;
        }
        else{
            return null;
        }
    }
    
    public function getTitulo($path = null,$user = null){
        if ($path){
            $indices = array('titulos',$path);
        }
        else {
            $indices = array('titulo');
        }
        $titulo = $this->getElemento($indices);
        if ($titulo){
            return str_replace('%user%',$user,$titulo);
        }
        else {
            return $this->getTituloDefecto();
        } 
    }
    
    public function getFooter($path = null, $user = null){
        if ($path){
            $indices = array('footers',$path);
        }
        else {
            $indices = array('footer');
        }
        $footer = $this->getElemento($indices);
        if ($footer){
            return str_replace('%user%',$user,$footer);
        }
        else {
            return $this->getFooterDefecto();
        }
    }
    
}
